﻿using System;
using System.Collections.Generic;

using System.Web;
using MYLSOFT.JsonSimple;
using System.IO;

namespace suce.upload
{
    static public class UploadHelper
    {

        static public string pathFiles = @"V:\";

        public static string GetPath(string folder1, string folder2, string filename)
        {
            string dir = pathFiles;

            if (!string.IsNullOrEmpty(folder1))
                dir = Path.Combine(dir, folder1);
            if (!string.IsNullOrEmpty(folder2))
                dir = Path.Combine(dir, folder2);
            Directory.CreateDirectory(dir);
            if (!string.IsNullOrEmpty(filename))
                dir = Path.Combine(dir, filename);

            return dir;
        }


        public static string[] ListFilesStringFullName(string folder, string filter)
        {
            string[] files = Directory.GetFiles(GetPath(folder, null, null), filter);
            return files;
        }

        public static string[] ListFilesStringFullName(string folder)
        {
            string[] files = Directory.GetFiles(GetPath(folder, null, null));
            return files;
        }

        public static string[] ListFilesString(string folder)
        {
            string[] files = Directory.GetFiles(GetPath(folder, null, null));
            for (int i = 0; i < files.Length; i++)
            {
                files[i] = Path.GetFileName(files[i]);
            }
            return files;
        }

        public static string[] ListFilesString(string folder, string filter)
        {
            string[] files = Directory.GetFiles(GetPath(folder, null, null), filter);
            for (int i = 0; i < files.Length; i++)
            {
                files[i] = Path.GetFileName(files[i]);
            }
            return files;
        }

        public static JSONArray ListFiles(string folder)
        {
            JSONArray ja = new JSONArray();
            string[] files = ListFilesString(folder);
            foreach (string it in files)
            {
                JSONArray ja1 = new JSONArray();
                ja1.Add(it);
                ja.Add(ja1);
            }
            return ja;
        }

        public static JSONArray ListFiles(string folder, string filter)
        {
            JSONArray ja = new JSONArray();
            string[] files = ListFilesString(folder, filter);
            foreach (string it in files)
            {
                JSONArray ja1 = new JSONArray();
                ja1.Add(it);
                ja.Add(ja1);
            }
            return ja;
        }

        public static void DeleteFile(string path)
        {
            File.Delete(path);
        }

        public static void deleteAll(String path)
        {
            try
            {
                if (Directory.Exists(path))
                {
                    Directory.Delete(path, true);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("Error : {0}", e.Message);
            }
        }

        public static void renameDir(String dirAnterior, String dirNuevo)
        {
            try
            {
                if (Directory.Exists(dirAnterior))
                {
                    String[] files = Directory.GetFiles(dirAnterior);
                    String archivo = String.Empty;
                    String archivoDestino = String.Empty;
                    foreach (String s in files)
                    {
                        archivo = Path.GetFileName(s);
                        archivoDestino = Path.Combine(dirNuevo, archivo);
                        System.IO.File.Copy(s, archivoDestino, true);
                    }
                    deleteAll(dirAnterior);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("Error : {0}", e.Message);
            }
        }
    }
}